package com.qingcheng.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.qingcheng.dao.OrderMapper;
import com.qingcheng.dao.TradeStatisticsMapper;
import com.qingcheng.pojo.order.TradeStatistics;
import com.qingcheng.service.order.TradeStatisticsService;
import com.qingcheng.util.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service(interfaceClass = TradeStatisticsService.class)
public class TradeStatisticsServiceImpl implements TradeStatisticsService {



    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private TradeStatisticsMapper tradeStatisticsMapper;

    @Autowired
    private IdWorker idWorker;



    /**
     * 添加统计数据
     * 从订单主表中查询数据 添加到
     *
     */
    @Transactional
    @Override
    public void tradeStatistics(LocalDate date) {
        //id;             //id-----------
        //browsePeople;   //浏览人数(暂时使用假数据)
        //ordersPeople;   //下单人数(同一个人可以下多个订单)
        //paymentPeople;  //付款人数(已付款订单用户数)
        //allOrderNum;    //订单数(订单可包含多件商品)
        //allGoodsNum;    //下单件数(所有订单的总商品数)
        //orderSuccessfulNum; //退款金额
        //placeMoney;     //下单金额
        //refundMoney;    //退款金额
        //payOrderNum;    //付款订单数
        //payGoodsNum;    //付款件数
        //payMoney;       //付款金额
        //startTime;      //统计的数据是哪一天----------
        TradeStatistics tradeStatistics = new TradeStatistics();
        //浏览人数 下单人数 付款人数
        tradeStatistics.setBrowsePeople(1000);//假数据
        tradeStatistics.setOrdersPeople(orderMapper.findOrdersPeopleNum(date).size());
        tradeStatistics.setPaymentPeople(orderMapper.findPaymentPeopleNum(date).size());

        //订单总数,有效订单数,付款订单数
        tradeStatistics.setAllOrderNum(orderMapper.findAllOrderNum(date));
        tradeStatistics.setOrderSuccessfulNum(orderMapper.findOrderSuccessNum(date));
        tradeStatistics.setPayOrderNum(orderMapper.findPayOrderNum(date));

        //商品总数,付款商品总数
        Integer allGoodsNum = orderMapper.findAllGoodsNum(date);
        if(allGoodsNum==null){ allGoodsNum = 0; }
        tradeStatistics.setAllGoodsNum(allGoodsNum);

        Integer payGoodsNum = orderMapper.findPayGoodsNum(date);
        if(payGoodsNum==null){ payGoodsNum = 0; }
        tradeStatistics.setPayGoodsNum(payGoodsNum);

        //订单总额,退款总额,付款总额

        Integer placeMoney = orderMapper.findPlaceMoney(date);
        if(placeMoney==null){ placeMoney=0; }
        tradeStatistics.setPlaceMoney(placeMoney);

        Integer refundMoney = orderMapper.findRefundMoney(date);
        if(refundMoney==null){ refundMoney=0; }
        tradeStatistics.setRefundMoney(refundMoney);

        Integer payMoney = orderMapper.findPayMoney(date);
        if(payMoney==null){ payMoney=0; }
        tradeStatistics.setPayMoney(payMoney);


        tradeStatistics.setId(idWorker.nextId()+"");//id

        Date today = new Date();
        long milliSecond = today.getTime()-(24*60*60*1000);
        Date yesterday = new Date();
        yesterday.setTime(milliSecond);
        tradeStatistics.setCreateTime(today);//统计时间
        tradeStatistics.setStartTime(yesterday);
        tradeStatisticsMapper.insert(tradeStatistics);
    }


    /**
     * 使用定时任务添加数据
     */
    @Override
    public void createData() {
        LocalDate localDate=LocalDate.now().minusDays(1);
        tradeStatistics(localDate);
    }

    /**
     * 查询所有数据(倒序排列)
     * @return
     */

    @Override
    public List<TradeStatistics> findAllData() {
        Example example = new Example(TradeStatistics.class);
        example.setOrderByClause("id DESC");
        return tradeStatisticsMapper.selectByExample(example);
    }

    /**
     * 查询30天数据
     * @return
     */
    @Override
    public List<TradeStatistics> findOneMonth() {
        List<TradeStatistics> allData = findAllData();
        List<TradeStatistics> oneMonth = new ArrayList<TradeStatistics>();
        for (int i = 0; i < 30 && i < allData.size(); i++) {
            oneMonth.add(allData.get(i));
        }
        return oneMonth;
    }

}
